#include "theme_manager.h"
#include <QStyle>
#include <QPalette>
#include <QApplication>

ThemeManager* ThemeManager::s_instance = nullptr;

ThemeManager::ThemeManager(QObject *parent)
    : QObject(parent)
    , m_currentTheme(Light)
{
    s_instance = this;
}

ThemeManager* ThemeManager::instance()
{
    return s_instance;
}

ThemeManager::Theme ThemeManager::currentTheme() const
{
    return m_currentTheme;
}

void ThemeManager::setTheme(Theme theme)
{
    if (m_currentTheme == theme)
        return;
        
    m_currentTheme = theme;
    
    // 应用主题到整个应用程序
    QPalette palette;
    
    if (theme == Dark) {
        // 深色主题
        palette.setColor(QPalette::Window, QColor(45, 45, 45));
        palette.setColor(QPalette::WindowText, Qt::white);
        palette.setColor(QPalette::Base, QColor(30, 30, 30));
        palette.setColor(QPalette::AlternateBase, QColor(45, 45, 45));
        palette.setColor(QPalette::ToolTipBase, QColor(45, 45, 45)); // 深色背景
        palette.setColor(QPalette::ToolTipText, Qt::white);          // 白色文字
        palette.setColor(QPalette::Text, Qt::white);
        palette.setColor(QPalette::Disabled, QPalette::Text, QColor(128, 128, 128));
        palette.setColor(QPalette::Button, QColor(53, 53, 53));
        palette.setColor(QPalette::ButtonText, Qt::white);
        palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(128, 128, 128));
        palette.setColor(QPalette::BrightText, Qt::red);
        palette.setColor(QPalette::Link, QColor(42, 130, 218));
        palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
        palette.setColor(QPalette::HighlightedText, Qt::black);
        palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(128, 128, 128));
    } else {
        // 浅色主题（默认）
        palette = QApplication::style()->standardPalette();
    }
    
    QApplication::setPalette(palette);
    emit themeChanged(theme);
}

QColor ThemeManager::color(const QString &role) const
{
    if (m_currentTheme == Dark) {
        if (role == "window") return QColor(45, 45, 45);
        if (role == "windowText") return Qt::white;
        if (role == "base") return QColor(30, 30, 30);
        if (role == "alternateBase") return QColor(45, 45, 45);
        if (role == "button") return QColor(53, 53, 53);
        if (role == "buttonText") return Qt::white;
        if (role == "text") return Qt::white;
        if (role == "highlight") return QColor(42, 130, 218);
    }
    // 浅色主题使用默认颜色
    return QColor();
}

QPalette ThemeManager::palette() const
{
    QPalette palette;
    
    if (m_currentTheme == Dark) {
        palette.setColor(QPalette::Window, QColor(45, 45, 45));
        palette.setColor(QPalette::WindowText, Qt::white);
        palette.setColor(QPalette::Base, QColor(30, 30, 30));
        palette.setColor(QPalette::AlternateBase, QColor(45, 45, 45));
        palette.setColor(QPalette::ToolTipBase, QColor(45, 45, 45)); // 深色背景
        palette.setColor(QPalette::ToolTipText, Qt::white);          // 白色文字
        palette.setColor(QPalette::Text, Qt::white);
        palette.setColor(QPalette::Disabled, QPalette::Text, QColor(128, 128, 128));
        palette.setColor(QPalette::Button, QColor(53, 53, 53));
        palette.setColor(QPalette::ButtonText, Qt::white);
        palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(128, 128, 128));
        palette.setColor(QPalette::BrightText, Qt::red);
        palette.setColor(QPalette::Link, QColor(42, 130, 218));
        palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
        palette.setColor(QPalette::HighlightedText, Qt::black);
        palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(128, 128, 128));
    }
    
    return palette;
}